/*
 * Copyright 2023-2025 Licensed under the Dual Licensing
 * website: https://aizuda.com
 */
package com.aizuda.bpm.beetlsql.impl;

import com.aizuda.bpm.beetlsql.mapper.FlwHisTaskActorMapper;
import com.aizuda.bpm.engine.dao.FlwHisTaskActorDao;
import com.aizuda.bpm.engine.entity.FlwHisTaskActor;
import com.aizuda.bpm.engine.entity.FlwTaskActor;
import com.github.yitter.idgen.YitIdHelper;

import java.util.List;

/**
 * 历史任务参与者数据访问层接口实现类
 *
 * <p>
 * <a href="https://aizuda.com">官网</a>尊重知识产权，不允许非法使用，后果自负，不允许非法使用，后果自负
 * </p>
 *
 * @author hubin
 * @since 1.0
 */
public class FlwHisTaskActorDaoImpl implements FlwHisTaskActorDao {
    private final FlwHisTaskActorMapper hisTaskActorMapper;

    public FlwHisTaskActorDaoImpl(FlwHisTaskActorMapper hisTaskActorMapper) {
        this.hisTaskActorMapper = hisTaskActorMapper;
    }

    @Override
    public boolean insert(FlwHisTaskActor hisTaskActor) {
        hisTaskActor.setId(YitIdHelper.nextId());
        return hisTaskActorMapper.createQuery().insertSelective(hisTaskActor) > 0;
    }

    @Override
    public boolean deleteByInstanceIds(List<Long> instanceIds) {
        return hisTaskActorMapper.createLambdaQuery().andIn(FlwTaskActor::getInstanceId, instanceIds).delete()>0;
    }

    @Override
    public boolean deleteByTaskId(Long taskId) {
        return hisTaskActorMapper.createLambdaQuery().andEq(FlwHisTaskActor::getTaskId, taskId).delete()>0;
    }

    @Override
    public List<FlwHisTaskActor> selectCcTaskActorsByInstanceId(Long instanceId) {
        return hisTaskActorMapper.createLambdaQuery().andEq(FlwHisTaskActor::getInstanceId, instanceId)
                .andEq(FlwHisTaskActor::getWeight, 6).select();
    }

    @Override
    public List<FlwHisTaskActor> selectListByInstanceId(Long instanceId) {
        return hisTaskActorMapper.createLambdaQuery()
                .andEq(FlwHisTaskActor::getInstanceId, instanceId).select();
    }

    @Override
    public List<FlwHisTaskActor> selectListByTaskId(Long taskId) {
        return hisTaskActorMapper.createLambdaQuery().andEq(FlwHisTaskActor::getTaskId, taskId).select();
    }

    @Override
    public List<FlwHisTaskActor> selectListByTaskIds(List<Long> taskIds) {
        return hisTaskActorMapper.createLambdaQuery().andIn(FlwHisTaskActor::getTaskId, taskIds).select();
    }

    @Override
    public List<FlwHisTaskActor> selectListByTaskIdAndActorId(Long taskId, String actorId) {
        return hisTaskActorMapper.createLambdaQuery().andEq(FlwHisTaskActor::getTaskId, taskId)
                .andEq(FlwHisTaskActor::getActorId, actorId).select();
    }
}
